'use strict';

const EMAIL_RE = /^[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+(?:\.[a-z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)*@(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?$/;
const USERNAME_RE = /^[a-z][a-z0-9_]+[a-z0-9]$/i;
// app.js
module.exports = app => {
  app.validator.addRule('username', (rule, value) => {
    let error
    if (!EMAIL_RE.test(value)) {
      error = 'username must be either email or string with lowercase, underline and integer';
      if (!USERNAME_RE.test(value)) {
        return error;
      } else {
        error = undefined;
      }
    }

    return error;
  });

  app.beforeStart(function* () {
  });
};
